package com.gxa.xly2024.service.impl;

import com.gxa.xly2024.dto.DTO;
import com.gxa.xly2024.mapper.PurchaseXinxiMapper;
import com.gxa.xly2024.pojo.PurchaseXinxi;
import com.gxa.xly2024.vo.VO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

@Service
public class PurchaseXinxiServiceImpl {
    
    @Resource
    private PurchaseXinxiMapper purchaseMapperXinxi;

    public Map<String, Object> selectPurchaseXinxiById(Integer id) {
        PurchaseXinxi purchaseXinxi = this.purchaseMapperXinxi.selectPurchaseXinxiById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询id成功");
        map.put("data", purchaseXinxi);
        return map;
    }

    public Map<String, Object> selectPurchaseXinxiByLimit(VO vo) {
        int index = (vo.getPage() - 1) * vo.getLimit();
        DTO dto = new DTO();  // alt+enter自动补全
        // 快速copy属性到指定对象中
        BeanUtils.copyProperties(vo, dto);   // 这里代表的是，把vo和dto相同属性的值，copy过去
        dto.setIndex(index);

        List<PurchaseXinxi> purchaseXinxi = this.purchaseMapperXinxi.selectPurchaseXinxiByLimit(dto);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "分页查询成功");
        map.put("count", this.purchaseMapperXinxi.selectCount(dto));   // 查询当前表总条数
        map.put("data", purchaseXinxi);
        return map;
    }



    public Map<String, Object> insert(PurchaseXinxi purchaseXinxi) {
        this.purchaseMapperXinxi.insert(purchaseXinxi);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增用户成功");
        map.put("data", purchaseXinxi);
        return map;
    }

    public Map<String, Object> updateById(PurchaseXinxi purchaseXinxi) {
        this.purchaseMapperXinxi.updateById(purchaseXinxi);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新用户成功");
        map.put("data", purchaseXinxi);
        return map;
    }

    public Map<String, Object> deleteById(Integer id) {
        this.purchaseMapperXinxi.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        map.put("id", id);
        return map;
    }
}
